package com.wejoy.video.middle.station.data.service.system.impl;

import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.system.IEvsUserMapper;
import com.wejoy.video.middle.station.data.domain.system.EvsUser;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.system.IEvsUserService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * evs用户同步
 *
 * @author admin
 * @date 2024年06月29日 17:04:26
 */
@Service
public class EvsUserServiceImpl implements IEvsUserService {
    @Autowired
    private IEvsUserMapper evsUserMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(EvsUser req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(EvsUser req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return evsUserMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(EvsUser evsUser) {
        WJAssert.notNull(evsUser, "入参对象不能为空");
        WJAssert.notNull(evsUser.getId(), "id参数错误");
        EvsUser where = new EvsUser();
        where.setId(evsUser.getId());
        return evsUserMapper.updateSelective(evsUser, where);
    }

    @Override
    public int updateSelectiveByPhone(EvsUser evsUser) {
        WJAssert.notNull(evsUser, "入参对象不能为空");
        WJAssert.notNull(evsUser.getTel(), "电话参数错误");
        EvsUser where = new EvsUser();
        where.setTel(evsUser.getTel());
        return evsUserMapper.updateSelective(evsUser, where);
    }

    @Override
    public int updateRoleSelectiveById(Long userId, String roleId) {
        return evsUserMapper.updateRoleSelectiveById(userId, roleId);
    }

    @Override
    public int getCount(EvsUser evsUser) {
        checkStringLength(evsUser, true);
        return evsUserMapper.getCount(evsUser);
    }

    @Override
    public EvsUser getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        EvsUser where = new EvsUser();
        where.setId(id);
        return evsUserMapper.getOne(where);
    }

    @Override
    public EvsUser getOne(EvsUser evsUser) {
        return evsUserMapper.getOne(evsUser);
    }

    @Override
    public List<EvsUser> getList(EvsUser evsUser) {
        checkStringLength(evsUser, true);
        return evsUserMapper.getList(evsUser);
    }

    @Override
    public List<EvsUser> getPageList(EvsUser evsUser) {
        checkStringLength(evsUser, true);
        return evsUserMapper.getPageList(evsUser);
    }

    @Override
    public void audit(Long id, String operator) {
        EvsUser req = new EvsUser();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        EvsUser req = new EvsUser();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        EvsUser req = new EvsUser();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void deleteByTime(Date date) {
        EvsUser req = new EvsUser();
        evsUserMapper.deleteByTime(date);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(EvsUser evsUser, boolean nullAble) {
        WJAssert.notNull(evsUser, "入参对象不能为空");
        WJAssert.limitMaxLength(evsUser.getTel(), true, 11, "手机不合法!");
        WJAssert.limitMaxLength(evsUser.getUserName(), true, 255, "用户名不合法!");
        WJAssert.limitMaxLength(evsUser.getDepartmentName(), true, 255, "部门不合法!");
        WJAssert.limitMaxLength(evsUser.getOperator(), true, 100, "操作人不合法!");
        WJAssert.limitMaxLength(evsUser.getRemark(), true, 255, "备注不合法!");
    }
}
